package com.wva.common.config;

import com.wva.common.constant.AuthenticationConst;
import com.wva.common.constant.LogConst;
import com.wva.common.context.UserContext;
import feign.RequestInterceptor;
import org.slf4j.MDC;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FeignConfig {
    /**
     * feign请求拦截器
     */
    @Bean
    RequestInterceptor requestInterceptor() {
        return requestTemplate -> {
            //将traceId写入请求头
            requestTemplate.header(LogConst.TRACE_ID_HEADER_NAME, MDC.get(LogConst.TRACE_ID_MDC_NAME));

            //将用户写入请求头
            if (UserContext.isLogin()) {
                requestTemplate.header(AuthenticationConst.LOGIN_USER_IN_HEADER, String.valueOf(UserContext.getUserId()));
            }

        };
    }
}
